package com.brightworks.scripthelper.model {
    import com.brightworks.scripthelper.analyzers.ScriptAnalyzer;
    import com.brightworks.scripthelper.constants.ApplicationStateConstants;
    import com.brightworks.scripthelper.constants.LanguageCodeConstants;
    import com.brightworks.util.text.PinyinProcessor;

    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class MainModel extends EventDispatcher {
        public var lessonDevelopmentWorkspace:LessonDevelopmentWorkspace;
        public var lessonLibraryList:Array;

        private var _pinyinProcessor:PinyinProcessor;
        private var _scriptAnalyzer:ScriptAnalyzer;
        private var _voiceScriptsInfo:VoiceScriptsInfo;

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        //
        //          Getters & Setters
        //
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

        private var _applicationState:String; // Use ApplicationStateConstants

        [Bindable (event="applicationStateChange")]
        public function get applicationState():String {
            return _applicationState;
        }

        private var _lessonXML:XML;

        public function get lessonXML():XML {
            return _lessonXML;
        }

        private var _mostRecentScriptCheckSuccessful:Boolean;

        [Bindable(event="mostRecentScriptCheckSuccessfulChange")]
        public function get mostRecentScriptCheckSuccessful():Boolean {
            return _mostRecentScriptCheckSuccessful;
        }

        private var _resultsString:String;

        [Bindable (event="resultsStringChange")]
        public function get resultsString():String {
            return _resultsString;
        }

        public function get voiceScriptsLanguageInfo_Chinese():VoiceScriptsLanguageInfo {
            return _voiceScriptsInfo.languageInfoList[LanguageCodeConstants.ISO_639_3_CODE_CHINESE_MANDARIN];
        }

        public function get voiceScriptsLanguageInfo_English():VoiceScriptsLanguageInfo {
            return _voiceScriptsInfo.languageInfoList[LanguageCodeConstants.ISO_639_3_CODE_ENGLISH];
        }

        private var _voiceScriptsViewState:String;

        [Bindable (event="voiceScriptsViewStateChange")]
        public function get voiceScriptsViewState():String {
            return _voiceScriptsViewState;
        }

        private var _voiceScriptsViewVisible:Boolean;

        [Bindable (event="voiceScriptsViewVisibleChange")]
        public function get voiceScriptsViewVisible():Boolean {
            return _voiceScriptsViewVisible;
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        //
        //          Public Methods
        //
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

        public function MainModel() {
            init();
        }

        public function processScriptCheck(script:String):void {
            script = _pinyinProcessor.convertNumberToneIndicatorsToToneMarks(script);
            _resultsString = _scriptAnalyzer.analyze(script);
            dispatchEvent(new Event("resultsStringChange"));
            updateApplicationState(ApplicationStateConstants.APPLICATION_STATE__SCRIPT_CHECK_RESULTS);
            updateVoiceScriptsViewVisible(!_scriptAnalyzer.doProblemReportsExist());
            var success:Boolean;
            if (_scriptAnalyzer.doProblemReportsExist()) {
                success = false;
                _lessonXML = null;
                _voiceScriptsInfo = null;
            } else {
                success = true;
                _lessonXML = _scriptAnalyzer.createLessonXML();
                _voiceScriptsInfo = _scriptAnalyzer.createVoiceScriptsInfo();
                updateVoiceScriptsViewState(ApplicationStateConstants.VOICE_SCRIPTS_VIEW_STATE__INITIAL);
            }
            if (success != _mostRecentScriptCheckSuccessful) {
                _mostRecentScriptCheckSuccessful = success;
                dispatchEvent(new Event("mostRecentScriptCheckSuccessfulChange"));
            }
        }

        public function updateApplicationState(value:String):void {
            _applicationState = value;
            dispatchEvent(new Event("applicationStateChange"));
        }

        public function updateVoiceScriptsViewState(value:String):void {
            _voiceScriptsViewState = value;
            dispatchEvent(new Event("voiceScriptsViewStateChange"));
        }

        public function updateVoiceScriptsViewVisible(value:Boolean):void {
            _voiceScriptsViewVisible = value;
            dispatchEvent(new Event("voiceScriptsViewVisibleChange"));
        }

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
        //
        //          Private Methods
        //
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

        private function init():void {
            _pinyinProcessor = new PinyinProcessor();
            _scriptAnalyzer = new ScriptAnalyzer();
            initLessonLibraryList();
        }

        private function initLessonDevelopmentWorkspace():void {
            lessonDevelopmentWorkspace = new LessonDevelopmentWorkspace();
            lessonDevelopmentWorkspace.folderPath = "C:\_lmentor_lessons";
        }

        private function initLessonLibraryList():void {
            var library:LessonLibrary;
            var libraryIdList:Array = ["com.languagecollaborative", "info.renhuibo", "info.xiaina", "info.zhangqiwei"];
            for each (var libraryId:String in libraryIdList) {
                library = new LessonLibrary();
                library.libraryId = libraryId;
                lessonLibraryList.push(library);
            }
        }

    }
}
